/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxcustomerexptapi.p                              */
/* PROCEDURE TYPE        : API export Process                               */
/* DESCRIPTION           : customer export Process                          */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                                28-Jul-24  */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{xxjson.i}
{xxcimload.i}

&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.


DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "no"      no-undo.
define variable v_site                       like si_site   no-undo.

define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus      as logical initial yes      no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.

define variable v_entity              like si_entity              no-undo.
define variable v_ee                  as logic                    no-undo.

define variable vi_filecount          as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram        as character                no-undo.
define variable vc_traceid            as character                no-undo.


 define temp-table ttSearchCondition no-undo
        field customer_from         like Debtor.DebtorCode
        field customer_to           like Debtor.DebtorCode
        .


 define temp-table ttcustomer no-undo

        field SharedSet_ID                like Debtor.SharedSet_ID
        field Debtor_ID                 like Debtor.Debtor_ID
        field DebtorCode                like Debtor.DebtorCode
        field DebtorIsActive            like Debtor.DebtorIsActive
        field BusinessRelation_ID         like Debtor.BusinessRelation_ID
        field DebtorType_ID             like Debtor.DebtorType_ID
        field DebtorIsTaxable           like Debtor.DebtorIsTaxable
        field TxuTaxUsage                 like Debtor.TxuTaxUsage
        field TxclTaxCls                  like Debtor.TxclTaxCls
        field DebtorTypeCode            like DebtorType.DebtorTypeCode
        field DebtorTypeDescription     like DebtorType.DebtorTypeDescription
        field DebtorTypeIsActive        like DebtorType.DebtorTypeIsActive
        field BusinessRelationCode        like Businessrelation.BusinessRelationCode
        field BusinessRelationName1       like Businessrelation.BusinessRelationName1
        field BusinessRelationName2       like Businessrelation.BusinessRelationName2
        field BusinessRelationName3       like Businessrelation.BusinessRelationName3
        field BusinessRelationSearchName  like Businessrelation.BusinessRelationSearchName
        field Address_ID                  like Address.Address_ID
        field AddressSearchName           like Address.AddressSearchName
        field AddressName                 like Address.AddressName
        field AddressType_ID              like Address.AddressType_ID
        field AddressStreet1              like Address.AddressStreet1
        field AddressStreet2              like Address.AddressStreet2
        field AddressStreet3              like Address.AddressStreet3
        field Country_ID                  like Address.Country_ID
        field AddressCity                 like Address.AddressCity
        field AddressState                like Address.AddressState
        field AddressTelephone            like Address.AddressTelephone
        field AddressFax                  like Address.AddressFax
        field AddressZip                  like Address.AddressZip
        field AddressEMail                like Address.AddressEMail
        field CountryCode                 like Country.CountryCode
        field CountryDescription          like Country.CountryDescription
        
          index crid Debtor_ID.
        
 define temp-table ttcontact no-undo
        field Contact_ID                  like Contact.Contact_ID
        field BusinessRelation_ID         like Contact.BusinessRelation_ID
        field Address_ID                  like Contact.Address_ID
        field DebtorCode                like Debtor.DebtorCode
        field AddressName                 like Address.AddressName
        field ContactName                 like Contact.ContactName
        field ContactEmail                like Contact.ContactEmail
        field ContactFunction             like Contact.ContactFunction
        field ContactFax                  like Contact.ContactFax
        field ContactGender               like Contact.ContactGender
        field ContactIsSecondary          like Contact.ContactIsSecondary
        field ContactIsPrimary            like Contact.ContactIsPrimary
        field ContactMobilePhone          like Contact.ContactMobilePhone
        field ContactTitle                like Contact.ContactTitle
         
      index crid DebtorCode.
      
   DEFINE DATASET dsSearchCondition FOR ttSearchCondition.

   hdataset = dataset dsSearchCondition:HANDLE.   
           
   DEFINE DATASET dsCustomer FOR ttcustomer.

   hresult = dataset dsCustomer:HANDLE.    
                

  
mainloop:
repeat on error undo mainloop, leave mainloop:
  
 
   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=no &archive=YES}

assign m_outpath     = m_outpath + "cm/"
       m_archpath    = m_archpath + "cm/"
       m_successpath = m_successpath + "cm/"
       m_errorpath   = m_errorpath + "cm/"
       m_workpath    = m_workpath + "cm/".


   empty temp-table ttcustomer.
   
   
   /* Load SEARCH conditions from JSON  */ 
   
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
                    
  if vl_RetOK then do:

   for first domains where domains.domaincode = global_domain no-lock,
         each domainsharedset of domains no-lock,
         first SharedSet of domainsharedset 
          where sharedset.sharedsettypecode = "Debtor" no-lock.
    for each ttSearchCondition.      
    
    if ttSearchCondition.customer_to = "" then ttSearchCondition.customer_to = hi_char.
    
     for each Debtor no-lock where Debtor.SharedSet_ID = SharedSet.SharedSet_ID  
                                 and Debtor.Debtorcode >= ttSearchCondition.customer_from
                                 and Debtor.Debtorcode <= ttSearchCondition.customer_to ,                               
         each Businessrelation of Debtor no-lock,
         each address of Businessrelation no-lock,
         each addresstype of address where addresstypecode = "HEADOFFICE" /* 只获取总部地址 */,
         each country of address no-lock.
         .
        find Debtortype of Debtor no-lock no-error.  /* 客户类型可能不存在 */
       
        
        create ttcustomer.
        
        assign ttcustomer.SharedSet_ID               = Debtor.SharedSet_ID
               ttcustomer.Debtor_ID                = Debtor.Debtor_ID
               ttcustomer.DebtorCode               = Debtor.DebtorCode
               ttcustomer.DebtorIsActive           = Debtor.DebtorIsActive
               ttcustomer.BusinessRelation_ID        = Debtor.BusinessRelation_ID
               ttcustomer.DebtorType_ID            = Debtor.DebtorType_ID
               ttcustomer.DebtorIsTaxable          = Debtor.DebtorIsTaxable
               ttcustomer.TxuTaxUsage                = Debtor.TxuTaxUsage
               ttcustomer.TxclTaxCls                 = Debtor.TxclTaxCls
              
               ttcustomer.DebtorType_ID            = if available Debtortype then DebtorType.DebtorType_ID else 0
               ttcustomer.DebtorTypeCode           = if available Debtortype then DebtorType.DebtorTypeCode  else ""
               ttcustomer.DebtorTypeDescription    = if available Debtortype then DebtorType.DebtorTypeDescription else ""
               ttcustomer.DebtorTypeIsActive       = if available Debtortype then DebtorType.DebtorTypeIsActive else false
               ttcustomer.BusinessRelationCode       = Businessrelation.BusinessRelationCode
               ttcustomer.BusinessRelationName1      = Businessrelation.BusinessRelationName1
               ttcustomer.BusinessRelationName2      = Businessrelation.BusinessRelationName2
               ttcustomer.BusinessRelationName3      = Businessrelation.BusinessRelationName3
               ttcustomer.BusinessRelationSearchName = Businessrelation.BusinessRelationSearchName
               ttcustomer.Address_ID                 = Address.Address_ID
               ttcustomer.AddressSearchName          = Address.AddressSearchName
               ttcustomer.AddressName                = Address.AddressName
               ttcustomer.AddressType_ID             = Address.AddressType_ID
               ttcustomer.AddressStreet1             = Address.AddressStreet1
               ttcustomer.AddressStreet2             = Address.AddressStreet2
               ttcustomer.AddressStreet3             = Address.AddressStreet3
               ttcustomer.Country_ID                 = Address.Country_ID
               ttcustomer.AddressCity                = Address.AddressCity
               ttcustomer.AddressState               = Address.AddressState
               ttcustomer.AddressTelephone           = Address.AddressTelephone
               ttcustomer.AddressFax                 = Address.AddressFax
               ttcustomer.AddressZip                 = Address.AddressZip
               ttcustomer.AddressEMail               = Address.AddressEMail
               ttcustomer.CountryCode                = Country.CountryCode
               ttcustomer.CountryDescription         = Country.CountryDescription
               .
               

     end. /*  for each Debtor no-lock  */
     end. /* for each ttSearchCondition. */     
          
   end. /* for first domains where */
   


  end.  /* if vl_RetOK then do: */ 
                    
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
         vc_topath " " skip 
         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */        
      
   
   
   leave.
end. /* repeat on error undo mainloop, leave mainloop: */
